public class BM10 {

public static class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        int length1 = 0;
        int length2 = 0;
        ListNode cur = pHead1;
        while(cur != null){
            length1++;
            cur = cur.next;
        }
        cur = pHead2;
        while(cur != null){
            length2++;
            cur = cur.next;
        }
        ListNode fast;
        ListNode slow ;
        if(length1 > length2){
            fast = pHead1;
            slow = pHead2;
        }else{
            fast = pHead2;
            slow = pHead1;
        }
        int k = Math.abs(length1 - length2);
        while(k != 0){
            fast = fast.next;
            k--;
        }
        while(fast != slow && fast != null){
            fast = fast.next;
            slow = slow.next;
        }
        return fast;


    }
}
